package com.toune.gdlocation.location

import android.content.Context
import android.util.Log
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import java.text.SimpleDateFormat
import java.util.*

/**
 * @Author Dong Lei
 * @Date 2021/1/29 0029-下午 13:39
 * @Info 描述：
 */
class GDLocationUtil : AMapLocationListener {

    companion object {
        var location: AMapLocation? = null
        private var gdLocationUtil: GDLocationUtil? = null
        fun initLocation(context: Context, mOnLocChangeListener: OnLocChangeListener) {
            if (gdLocationUtil == null) {
                gdLocationUtil = GDLocationUtil(context, mOnLocChangeListener)
            }
        }

        fun mLocation(mOnLocChangeListener: OnLocChangeListener?): AMapLocation? {
            if (mOnLocChangeListener != null && gdLocationUtil != null) {
                gdLocationUtil!!.onLocChangeListener = mOnLocChangeListener
            }
            return location
        }
    }

    var onLocChangeListener: OnLocChangeListener? = null

    constructor(context: Context, mOnLocChangeListener: OnLocChangeListener) {
        onLocChangeListener = mOnLocChangeListener
        mlocationClient = AMapLocationClient(context);
        //初始化定位参数
        mLocationOption = AMapLocationClientOption();
        //设置定位监听
        mlocationClient!!.setLocationListener(this);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption!!.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy;
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption!!.interval = 2000;
        //设置定位参数
        mlocationClient!!.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        mlocationClient!!.startLocation();
    }

    //声明mlocationClient对象
    var mlocationClient: AMapLocationClient? = null

    //声明mLocationOption对象
    var mLocationOption: AMapLocationClientOption? = null

    override fun onLocationChanged(amapLocation: AMapLocation?) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() === 0) {
                location = amapLocation
                //定位成功回调信息，设置相关消息
                amapLocation.locationType //获取当前定位结果来源，如网络定位结果，详见定位类型表
                amapLocation.latitude //获取纬度
                amapLocation.longitude //获取经度
                amapLocation.accuracy //获取精度信息
                val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                val date = Date(amapLocation.time)
                df.format(date) //定位时间
                if (onLocChangeListener != null) {
                    onLocChangeListener!!.onChange(amapLocation)
                }
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e(
                    "AmapError", ("location Error, ErrCode:"
                            + amapLocation.getErrorCode()) + ", errInfo:"
                            + amapLocation.getErrorInfo()
                )
            }
        }
    }

    interface OnLocChangeListener {
        fun onChange(location: AMapLocation)
    }
}